package swing.dialog;

import api.MainServer;
import beans.Group;
import client.UserSession;
import com.syuchen.protocol.http.Response;
import swing.FrameManager;
import swing.customComp.CustomStyles;
import swing.customComp.HintJTextField;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;

/**
 * 注册对话框
 */
public class AddWhiteboardDialog extends JDialog {

    public JPanel addWhiteboardPanel;

    private JLabel whiteboardStaticText;
    private HintJTextField whiteboardField;
    private JButton addButton;

    private ActionListener registerActionListener;

    public AddWhiteboardDialog(JFrame parentFrame) {
        this.setTitle("添加白板");
        this.setSize(400,300);
        this.setResizable(false);
        this.setLocationRelativeTo(parentFrame);
        addWhiteboardPanel = new JPanel();
        addWhiteboardPanel.setLayout(null);
        initAllComponent();
        addAllComponent();
        SwingUtilities.invokeLater(() -> addButton.requestFocusInWindow());
        this.setContentPane(addWhiteboardPanel);
    }

    private void initAllComponent() {
        whiteboardStaticText = new JLabel("输入白板名字：");
        Font staticTextFont = new Font("宋体", Font.BOLD,16);
        whiteboardStaticText.setFont(staticTextFont);
        whiteboardStaticText.setBounds(30,20,200,20);
        //账户输入框
        whiteboardField = new HintJTextField();
        whiteboardField.setBounds(30, 45, 285, 35);
        whiteboardField.setFont(CustomStyles.inputFont);
        whiteboardField.setHintText("请输入4-24位字符的白板名字");


        //注册按钮
        addButton = new JButton("添加");
        addButton.setBounds(30, 150, 285, 35);
        registerActionListener = listen -> {
            String group_name = whiteboardField.getText().trim();
            new Thread(() -> {
                addWhiteboard(group_name);
            }).start();
        };
        addButton.addActionListener(registerActionListener);
    }

    private void addAllComponent() {
        addWhiteboardPanel.add(whiteboardStaticText);
        addWhiteboardPanel.add(whiteboardField);
        addWhiteboardPanel.add(addButton);
    }

    private void enterAddGroupState() {
        SwingUtilities.invokeLater(() -> {
            addButton.setText("添加中...");
            addButton.removeActionListener(registerActionListener);
            whiteboardField.setEnabled(false);
        });
    }

    private void exitAddGroupState() {
        SwingUtilities.invokeLater(() -> {
            addButton.setText("添加");
            addButton.addActionListener(registerActionListener);
            whiteboardField.setEnabled(true);
        });
    }

    public void addWhiteboard(String whiteboard_name) {
        if (whiteboard_name.isEmpty()) {
            showMessageDialog("输入不能为空！");
            return;
        }
        if (whiteboard_name.length() < 4 || whiteboard_name.length() > 24) {
            showMessageDialog("名字长度不合法！");
            return;
        }

        try {
            this.enterAddGroupState();
            Group group = UserSession.getSelectedGroup();
            Response response = MainServer.groupAction.add_whiteboard(group.getId(), whiteboard_name);
            if (response.getStatus_code() != Response.OK) {
                showMessageDialog(response.getStatus_code() + " " + response.getStatus_description());
                return;
            }
            FrameManager.mainFrame.updateWhiteboardList();
            showMessageDialog("添加成功");
            this.dispose();
        } catch (Exception e) {
            showMessageDialog("服务器连接失败！");
            e.printStackTrace();
        }  finally {
            SwingUtilities.invokeLater(this::exitAddGroupState);
        }
        FrameManager.mainFrame.updateWhiteboardList();
    }

    private void showMessageDialog(String message) {
        JOptionPane.showMessageDialog(addWhiteboardPanel, message);
    }

    private int showConfirmDialog(String message) {
        return JOptionPane.showConfirmDialog(addWhiteboardPanel, message, "提示", JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);
    }
}
